package de.backtomedieval.activities.gamemenu;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import de.backtomedieval.R;
import de.backtomedieval.activities.TopBar;
import de.backtomedieval.activities.startscreen.ProfileStartscreen;
import de.backtomedieval.activities.startscreen.Startscreen;
import de.backtomedieval.main.Game;
import de.backtomedieval.scenarios.AbstractScenario;

public class Gamemenu extends Activity {

	private Handler mHandler = new Handler();
	private GridView grid_main;
	private int refresh = Game.getRefreshrate();
	private TopBar topbar;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.gamemenu_main);

		topbar = new TopBar(this);
		topbar.setTitle(Game.getGame().getcCity().getName() + ": "+ getString(R.string.titlegamemenu));

		grid_main = (GridView)findViewById(R.id.GridViewMenu);
		grid_main.setAdapter(new ImageAdapter(this));
        grid_main.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
            	onGameMenuChoose(position);
            }
        });
		
		topbar.refreshCityInfo();
	}

	protected void onGameMenuChoose(int position) {
		mHandler.removeCallbacks(mUpdateCityInfoTask);
		if (position==0) {
			onButtonBuild();
		} else if (position==1) {
			onButtonUnitBuild();
		} else if (position==2) {
			onButtonViewUnitsInCity();
		} else if (position==3) {
			onButtonResearch();
		} else if (position==4) {
			onButtonMissionInfo();
		} else if (position==5) {
			onButtonSettings();
		} else if (position==6) {
			onButtonExitGame();
		} else if (position==7) {
			AbstractScenario s = Game.getGame().getScenario();
			if (s!=null) {
				s.onStartFight();
			}
		}		
		
	}

	private void onButtonMissionInfo() {
		Intent viewMission = new Intent(Gamemenu.this, ViewMission.class);
		startActivity(viewMission);
		this.finish();
	}

	private void onButtonResearch() {
		Intent researchIntent = new Intent(Gamemenu.this, Researchmenu.class);
		startActivity(researchIntent);
		this.finish();
	}

	protected void onButtonViewUnitsInCity() {
		Intent viewUnitsIntent = new Intent(Gamemenu.this, ViewUnits.class);
		startActivity(viewUnitsIntent);
		this.finish();
	}

	protected void onButtonBuild() {
		Intent buildmenuIntent = new Intent(Gamemenu.this, Buildmenu.class);
		startActivity(buildmenuIntent);
		this.finish();

	}

	protected void onButtonUnitBuild() {
		Intent unitbuildmenuIntent = new Intent(Gamemenu.this,
				UnitBuildMenu.class);
		startActivity(unitbuildmenuIntent);
		this.finish();

	}

	protected void onButtonSettings() {
		Intent setingsmenuIntent = new Intent(Gamemenu.this,
				SettingsScreen.class);
		startActivity(setingsmenuIntent);
		this.finish();
	}

	protected void onButtonExitGame() {
		Game.endGame();
		Intent startscreenIntent = new Intent(Gamemenu.this,
				ProfileStartscreen.class);
		startActivity(startscreenIntent);
		this.finish();
	}

	@Override
	public void onBackPressed() {
		// TODO: Abfrage.
		return;
	}
	
	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onPause() {
		super.onPause();
		mHandler.removeCallbacks(mUpdateCityInfoTask);
	}

	@Override
	public void onResume() {
		super.onResume();
		Game.setLastActivity(this);
		mHandler.removeCallbacks(mUpdateCityInfoTask);
		mHandler.postDelayed(mUpdateCityInfoTask, 100);
	}

	public class ImageAdapter extends BaseAdapter {
		Context mContext;
		public static final int ACTIVITY_CREATE = 10;

		public ImageAdapter(Context c) {
			mContext = c;
		}

		
		public int getCount() {
			AbstractScenario s =Game.getGame().getScenario();
			if (s!=null && s.addStartFight()) {
				return 8;
			} else {
				return 7;
			}
			
		}

		
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View v;
			if (convertView == null) {
				LayoutInflater li = getLayoutInflater();
				v = li.inflate(R.layout.gridiconlarge, null);
				TextView tv = (TextView) v.findViewById(R.id.icon_text);
				ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
				
				if (position == 0) {
					tv.setText(R.string.gamemenu_buttonbuild);
					iv.setImageResource(R.drawable.build);
				} else if (position == 1) {
					tv.setText(R.string.gamemenu_buttonunitbuild);
					iv.setImageResource(R.drawable.buildunits);
				} else if (position == 2) {
					tv.setText(R.string.gamemenu_buttonviewunitsincity);
					iv.setImageResource(R.drawable.city);
				} else if (position == 3) {
					tv.setText(R.string.gamemenu_buttonresearch);
					iv.setImageResource(R.drawable.research);
				} else if (position == 4) {
					tv.setText(R.string.gamemenu_buttonmission);
					iv.setImageResource(R.drawable.mission);
				} else if (position == 5) {
					tv.setText(R.string.gamemenu_buttonSettings);
					iv.setImageResource(R.drawable.settings);
				} else if (position == 6) {
					tv.setText(R.string.gamemenu_buttonExit);
					iv.setImageResource(R.drawable.exit);
				} else if (position == 7) {
					tv.setText(R.string.gamemenu_startFight);
					iv.setImageResource(R.drawable.icon);
				}
				

				
			}

			else {
				v = convertView;
			}
			return v;
		}

		
		public Object getItem(int position) {
			return null;
		}

		
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
	}


	private Runnable mUpdateCityInfoTask = new Runnable() {
		public void run() {
			Log.v("Update", "Gamemenu: Cityinfo");
			topbar.refreshCityInfo();
			mHandler.postDelayed(this, refresh);

		}
	};
}
